'use strict';
const winston = require("winston");
//日志对象列表
let logMap = {};

module.exports.init = (tp)=>{
	tp = tp || "http";
	if(tp in logMap) return logMap[tp];
	let log = winston.createLogger({
		format: winston.format.combine(
			winston.format.timestamp({format:"HH:mm:ss.SSS"})
		),
		levels: winston.config.syslog.levels,
	  transports: [
			new winston.transports.Console({level:'error', format:winston.format.combine(winston.format.colorize(),myPrint)}),
			new winston.transports.File({filename: 'logs/'+tp+'.log', format:myPrint, maxsize:10240000, maxFiles:100, prettyPrint:JSON.stringify})
	  ]
	});
	logMap[tp] = log;
	return log;
}

const myPrint = winston.format.printf(info => {
	let sp = Symbol.for('splat');
	if(info[sp]) return `${info.timestamp} ${info.level}: ${info.message} ${info[sp].join(' ')}`;
	else return `${info.timestamp} ${info.level}: ${info.message}`;
});